<?php
/********************************************************************
 *	Author:
 *		Jestery Liu <jestery@gmail.com>
 *		http://www.pte.cn
 *
 *	File:			FileSpec.class.php
 *	Create Date:	2007-02-15 19:24:42 CST
 *******************************************************************/

require_once(PTE::getCoreLibsPath().'FilenameParser.class.php');

class FileSpec
{
	protected $field;
	protected $filename;
	protected $realname;
	protected $extension;
	protected $filesize;
	protected $mimetype;
	protected $protocol;
	protected $url;
	protected $isRemote;
	protected $host;
	protected $port;
	protected $path;
	protected $query;
	protected $username;
	protected $password;

	public function __construct($field)
	{
		$this->field = $field;
		$this->parse($field);
	}

	protected function parse($field)
	{
		$this->cleanVars();
		if (is_array($field) && !empty($field['tmp_name']))
		{
			/* Form uploaded */
			$this->isRemote = false;
			$f = new FilenameParser($field['name']);
			$this->filename = $field['tmp_name'];
			$this->realname = $f->basename;
			$this->extension = $f->extension;
			$this->mimetype = $this->_getMimetype($this->filename);
			unset($f);
		}
		elseif (preg_match('#^(https?|ftp)://(.+)#iU',$field))
		{
			/* Remote file */
			$this->isRemote = true;
			$tmp = parse_url($field);
			$this->filename = $field;
			$this->protocol = $tmp['scheme'];
			$this->host = $tmp['host'];
			$this->path = $tmp['path'];
			$this->port = $tmp['port'];
			$this->query = $tmp['query'];
			$this->username = $tmp['user'];
			$this->password = $tmp['pass'];
			if (!empty($this->path))
			{
				$f = new FilenameParser($this->path);
				$this->realname = $f->basename;
				$this->extension = $f->extension;
			}
			else
			{
				$this->realname = '';
				$this->extension = '';
			}
			$tmp = array();
			foreach(explode('/', $this->path) as $v)
			{
				$tmp[] = urlencode($v);
			}
			$this->path = implode('/', $tmp);
			$this->url .= $this->protocol.'://';
			if (!empty($this->username))
			{
				$this->url .= $this->username;
				if (!empty($this->password))
					$this->url .= ":{$this->password}";
				$this->url .= '@';
			}
			$this->url .= $this->host;
			if (!empty($this->port))
				$this->url .= ":{$this->port}";
			$this->url .= $this->path;
			if (!empty($this->query))
				$this->url .= "?{$this->query}";
		}
		else
		{
			$f = new FilenameParser($field);
			$this->filename = $field;
			$this->realname = $f->basename;
			$this->extension = $f->extension;
			$this->path = $f->dirname;
			$this->mimetype = $this->_getMimetype($this->filename);
		}
		$this->filesize = $this->_getFilesize($this->filename);
	}

	protected function cleanVars()
	{
		unset($this->filename, $this->realname, $this->extension, $this->filesize, $this->mimetype);
		unset($this->protocol, $this->url, $this->isRemote, $this->host, $this->port, $this->path);
		unset($this->query, $this->username, $this->password);
	}

	protected function _getMimetype($filename)
	{
		// TODO: get mimetype in another way.
		$mimetype = mime_content_type($filename);
		if (!$mimetype && is_array($this->field) && array_key_exists('type', $this->field))
		{
			$mimetype = $this->field['type'];
			$mimetype = (strpos($mimetype, '; name') !== false) ? str_replace(strstr($mimetype, '; name'), '', $mimetype) : $mimetype;
		}
		if (!$mimetype)
			$mimetype = 'application/octet-stream';
		return $mimetype;
	}

	protected function _getFilesize($filename)
	{
		if ($size = @filesize($filename) === false)
			$size = -1;
		return $size;
	}

	protected function isImage()
	{
		return strpos($this->mimetype, 'image/')!==false;
	}

	public function getFilename() { return $this->filename; }
	public function getRealname() { return $this->realname; }
	public function getExtension() { return $this->extension; }
	public function getFilesize() { return $this->getFilesize(); }
	public function getMimetype() { return $this->mimetype; }
	public function getProtocol() { return $this->protocol; }
	public function getHost() { return $this->host; }
	public function getPort() { return $this->port; }
	public function getPath() { return $this->path; }
	public function getUsername() { return $this->username; }
	public function getPassword() { return $this->password; }
	public function getQuery() { return $this->query; }
}

/* vim: set ts=4 sw=4 ff=unix: */
